package alingrad.test.objects;

import javax.microedition.khronos.opengles.GL10;

import alingrad.engine.collisions.CollidableGameObject;
import alingrad.engine.collisions.CollisionMask;
import alingrad.engine.core.Game;
import alingrad.engine.core.GameProperties;
import alingrad.engine.graphics.primitives.RenderableSquare;
import alingrad.engine.util.GLColor;
import android.util.Log;

public class Square extends CollidableGameObject {

	private RenderableSquare mSquare;
	private boolean mMaskUpdated;
	private CollisionMask mMask;

	public void loadGraphics(GL10 gl) {
		mSquare = new RenderableSquare(20);
		mMask = new CollisionMask();
	}

	public Square(final int x, final int y) {
		super();
		mX = x;
		mY = y;
		mMaskUpdated = false;
	}

	@Override
	public void update(final long delta) {
		mX += 10 * delta / 1000f;
		mY += 10 * delta / 1000f;
		if (mX > GameProperties.WIDTH)
			mX = 0;
		if (mY > GameProperties.HEIGHT)
			mY = 0;
		mMaskUpdated = false;
		Game.sRenderManager.scheduleToRender(mSquare, mX, mY, GLColor.RED);
	}

	@Override
	public CollisionMask getMask() {
		if (!mMaskUpdated) {
			mMask.update(mSquare.getData(), mX, mY);
			mMaskUpdated = true;

		}
		return mMask;
	}

	@Override
	public void collidedWith(final CollidableGameObject other) {
		super.collidedWith(other);
		if (other instanceof TestSprite)
			Log.d(this.getClass().getSimpleName(), "collided with TestSprite");
	}
}
